package com.clkj.cldisabledmanclient.mvp.satisfactionresearch;

import android.provider.SyncStateContract;
import android.util.Log;

import com.clkj.cldisabledmanclient.bean.MyRecoverTrainBean;
import com.clkj.cldisabledmanclient.bean.SatisfationResearchBean;
import com.clkj.cldisabledmanclient.constant.Common;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.http.HttpService;
import com.clkj.cldisabledmanclient.mvp.myrecovertrain.MyRecoverTrainContract;
import com.clkj.cldisabledmanclient.util.DesUtil;
import com.clkj.cldisabledmanclient.util.LogUtil;
import com.clkj.cldisabledmanclient.util.scheduler.BaseSchedulerProvider;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;

/**
 * Created by clboys on 2019/5/15.
 */

public class SatisfationResearchPresenter implements SatisfationResearchContract.Presenter {


    @NonNull
    SatisfationResearchContract.View mView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeDisposable mDisposables;


    public SatisfationResearchPresenter(@NonNull SatisfationResearchContract.View view, @NonNull BaseSchedulerProvider mSchedulerProvider
            , @NonNull HttpService httpService) {
        this.mView = view;
        this.mSchedulerProvider = mSchedulerProvider;

        this.mDisposables = new CompositeDisposable();
        this.mHttpService = httpService;

        this.mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mDisposables.dispose();
        mDisposables = null;
    }

    @Override
    public void getSatisfationResearchList(String username, String page, String rows) {

        mHttpService.getQuestionnaireList(username, page, rows)
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Predicate<JsonObject>() {
                    @Override
                    public boolean test(@NonNull JsonObject jsonObject) throws Exception {
                        LogUtil.e("getSatisfationResearch:" + jsonObject.toString());


                        if (!jsonObject.get(Config.RESPONSE_KEY_CODE).getAsString().equals(Config.RESPONSE_STATUS_SUCCESS)) {
                            mView.dismissLoading();
                            mView.getSatisfationResearchListError(jsonObject.get(Common.RESPONSE_KEY_MSG).getAsString());
                        }


                        return jsonObject.get(Config.RESPONSE_KEY_CODE).getAsString().equals(Config.RESPONSE_STATUS_SUCCESS);

                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposables.add(d);
                    }

                    @Override
                    public void onNext(@NonNull JsonObject jsonObject) {

                        mView.dismissLoading();
                        String jsonStr = jsonObject.get(SyncStateContract.Constants.DATA).getAsString();
                        try {
                            jsonStr = DesUtil.decryptDESCBC(jsonStr, DesUtil.KEY);
                            Log.e("getSatisfationResearch2", jsonStr);
                            List<SatisfationResearchBean> satisfationResearchBeans = new Gson().fromJson(jsonStr, new TypeToken<List<SatisfationResearchBean>>() {
                            }.getType());
                            mView.getSatisfationResearchListSuccess(satisfationResearchBeans);

                        } catch (Exception e) {
                            e.printStackTrace();
                            mView.getSatisfationResearchListError(TipStr.TIP_SERVER_GET_DATA_WRONG_AND_TRY_LATER);

                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                        e.printStackTrace();

                        mView.dismissLoading();
                        mView.getSatisfationResearchListError(TipStr.TIP_SERVER_GET_DATA_WRONG_AND_TRY_LATER);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
